package com.apache.funds.service.plugins;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.funds.common.FundsCache;
import com.apache.funds.common.FundsToolsUtil;
import com.apache.funds.entity.SvmUser;
import com.apache.funds.manager.SvmUserManager;
import com.apache.funds.util.Constants;
import com.apache.funds.vo.UserFundsVo;

/**  
 * 从缓存获取我的资金余额
 * @author xhe 创建时间：2015-12-05         
 */
public class FundsMyCachePlugin implements PluginConnector {
	private Logger log = LoggerFactory.getLogger(getClass());
	private SvmUserManager svmUserManager;

	@Override
	public Object execute(ParamsVo arg0) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		entity.setMessage("操作失败");
		Map<String, String> prarmMap = arg0.getParams();
		//校验参数
		String userEname = prarmMap.get("userEname");
		if (Validator.isNull(userEname)) {
			entity.setEntity("false");
			entity.setMessage(Constants.Msg.F003);
			return entity;
		}

		UserFundsVo userFundsVo = FundsCache.getInstance().getBalanCache(userEname);
		if (userFundsVo != null) {
			Map<String, Object> entityMap = FundsToolsUtil.getInstance().toMapDelNull(userFundsVo);
			entity.setEntity(entityMap);
			entity.setMessage(Constants.Msg.F000+",查询完成");
		} else {
			//没找余额对下，则进行用户查询下，验证此用户是否存在账号；

			entity.setEntity("false");
			entity.setMessage(Constants.Msg.F001+",查询失败");

			//			ParamsVo<SvmUser> vo = new ParamsVo<SvmUser>();
			//			SvmUser userVo = new SvmUser();
			//			userVo.setUsername(userEname);
			//			vo.setObj(userVo);
			//			List<SvmUser> list = svmUserManager.getList(vo);
			//			if (list==null || list.size()<1) {

			ParamsVo<SvmUser> voU = new ParamsVo<SvmUser>();
			voU.setParams("userName", userEname);
			voU.setMethodKey("ByUserName");
			SvmUser selectUser = (SvmUser) svmUserManager.execute(voU);
			if (selectUser == null) {
				log.info("此[" + userEname + "]账号未开通虚拟账户");
			} else {
				//用户存在情况，可能是未初始化期初余额 ？？？？？
			}
		}
		return entity;
	}

	public void setSvmUserManager(SvmUserManager svmUserManager) {
		this.svmUserManager = svmUserManager;
	}

}
